package com.racetime.daoImpl;

import com.racetime.dao.DateDao;
import com.racetime.po.Date;
import com.racetime.util.JdbcUtil;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-24
 *
 * 日程表操作实现类
 */
public class DateDaoImpl implements DateDao {

    @Override
    public Date findDateById(int dateId) {
        List<Date> dateList = JdbcUtil.query("select * from t_date where date_id = ?", Date.class, dateId);
        return dateList.size() == 0 ? null : dateList.get(0);
    }

    @Override
    public List<Date> findDateByUserId(int userId) {
        List<Date> dateList = JdbcUtil.query("select * from t_date where user_id = ?", Date.class, userId);
        return dateList.size() == 0 ? null : dateList;
    }

    /**
     * 分别找出开始时间在里边的与终止时间在里边的，合起来就是全部要找的日程
     * @param userId 用户 id
     * @param startTime 指定的起始时间
     * @param endTime 指定的终止时间
     * @return 符合条件的日程集合
     */
    @Override
    public List<Date> findDateByTimeScope(int userId, String startTime, String endTime) {
        List<Date> dateList = JdbcUtil.query("select date_id from t_date " +
                "where date_start >= ? and date_start <= ? and date_end >= ? and date_end <= ?", Date.class,
                startTime, endTime, startTime, endTime);
        return dateList.size() == 0 ? null : dateList;
    }

    @Override
    public Date findDateByTime(int userId, String time) {
        List<Date> dateList = JdbcUtil.query("select * from t_date where date_start <= ? and date_end >= ?", Date.class,time, time);
        return dateList.size() == 0 ? null : dateList.get(0);
    }

    @Override
    public int createNewDate(Date date) {
        return JdbcUtil.insert("t_date", date);
    }

    @Override
    public boolean updateDate(Date date) {
        return JdbcUtil.update("t_date", "date_id = " + date.getDateId(), date);
    }

    @Override
    public boolean deleteDate(int dateId) {
        return JdbcUtil.delete("t_date", "date_id = " + dateId);
    }
}
